package com.sxit.tdf.server.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxit.tdf.server.entity.Dept;
import com.sxit.tdf.server.entity.bo.DeptInfoBo;
import com.sxit.tdf.server.entity.bo.SimpleDeptApproveFileBo;
import com.sxit.tdf.server.entity.vo.SiftDeptInfoVo;
import com.sxit.tdf.server.exception.TdfException;
import com.sxit.tdf.server.mapper.DeptMapper;
import com.sxit.tdf.server.service.IDeptService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 服务实现类
 *
 * @author 橙子高🍊
 * @since 2022-02-05
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
  @Override
  public IPage<DeptInfoBo> getDeptInfoList(SiftDeptInfoVo siftDeptInfoVo) {

    HashMap hashMap = JSONUtil.parse(siftDeptInfoVo).toBean(HashMap.class);

    Page<DeptInfoBo> page =
        new Page<DeptInfoBo>(siftDeptInfoVo.getCurrent(), siftDeptInfoVo.getPageSize());

    return baseMapper.getDeptInfoList(page, hashMap);
  }

  @Override
  public boolean deleteById(int id) {

    boolean delete = baseMapper.isDelete(id);
    // 判断是否可以删除
    // true 表示不可以删除
    // false 表示可以删除
    if (delete) {
      throw new TdfException("该系部下有用户存在");
    } else {
      return baseMapper.deleteById(id) == 1;
    }
  }

  @Override
  public List<SimpleDeptApproveFileBo> getDeptApproveFile() {
    return baseMapper.getDeptApproveFile();
  }
}
